package com.LeeCode;

import java.util.HashSet;
import java.util.Set;

/**
 * 删除后的最大子数组元素和
 */

public class Code3487 {
    public static void main(String[] args) {
        int[] nums = {1, 2, -1, -2, 1, 0, -1};
        System.out.println(new Code3487().maxSum(nums));
    }

    public int maxSum(int[] nums) {
        Set<Integer> set = new HashSet<>();
        int ans = 0, maxNeg = Integer.MIN_VALUE;
        for (int num : nums) {
            if (num > 0 && !set.contains(num)) {
                set.add(num);
                ans += num;
            }
            if (num <= 0)
                maxNeg = Math.max(maxNeg, num);
        }
        return ans == 0 ? maxNeg : ans;
    }

    public int maxSum1(int[] nums) {
        int ans = 0, maxNeg = Integer.MIN_VALUE;
        boolean[] cnt = new boolean[101];
        for (int num : nums) {
            if (num > 0 && !cnt[num]) {
                cnt[num] = true;
                ans += num;
            }
            if(num <= 0) maxNeg = Math.max(maxNeg, num);
        }
        return ans == 0 ? maxNeg : ans;
    }
}
